<?php

function showMemoryUsage()
{
	function showMemoryUsage_ByteSize($bytes) 
	{
		$size = $bytes / 1024;
		if($size < 1024)
		{
			$size = number_format($size, 2);
			$size .= ' KB';
		} 
		else 
		{
			if($size / 1024 < 1024) 
			{
				$size = number_format($size / 1024, 2);
				$size .= ' MB';
			} 
			else if ($size / 1024 / 1024 < 1024)  
			{
				$size = number_format($size / 1024 / 1024, 2);
				$size .= ' GB';
			}
		}
		return $size;
	} 
	
	echo '<br />Memory usage: <b>' . showMemoryUsage_ByteSize(memory_get_usage()) . '</b>';
}

function show($value)
{
	static $showInitialized;
	
	if(!$showInitialized)
	{
		echo "<style type=\"text/css\">
			.show-debug { margin: 12px; border: 1px solid #000000; }
			.show-debug * { white-space: pre; font-family: verdana, arial; list-style-type: none; margin: 0px; padding: 0px; line-height: 12px; font-size: 12px; line-height: 18px; }
			.show-debug a { height: 18px; line-height: 18px; color: #FFFFFF; padding-left: 12px; text-decoration: none; background-color: #2EA7DB; display: block; }
			.show-debug ul li a { background-color: #49ADD8; }
			.show-debug ul li ul li a { background-color: #57B0D6; }
			.show-debug ul li ul li ul li a { background-color: #6AB5D1; }
			.show-debug ul li ul li ul li ul li a { background-color: #77B7CE; }
			.show-debug ul { background-repeat: repeat; background-image: url('%3D%3D');}
			.show-debug ul li { background-color: #D7E7F4; display: block; }
			.show-debug ul li b { padding-left: 12px; }
			.show-debug ul li ul { padding-left: 18px; }
			.show-debug-info { padding: 3px; color: #898D8E; font-size: 10px; background-color: #E3F2F7; }
			</style>";
		echo '<script>function debugResultToggle($id){var div=document.getElementById($id); div.style.display=(div.style.display==\'none\')?\'\':\'none\';};</script>';
		$showInitialized = true;
	}

	$backtrace = debug_backtrace();
	
	$d = '';
	$d .= '<div class="show-debug">';
	$d .= '<div class="show-debug-info">Called from file "' . $backtrace[0]['file'] . '" at line ' . $backtrace[0]['line'] . '</div>';
	if(count($value))
	{
		$d .= showInner($value);
	}
	else
	{
		$d .= '<ul><li>empty</li></ul>';
	}
	$d .= '</div>';

	echo $d;
}

function showInner($value, $ulId=null)
{
	$id = $ulId ? " id=\"{$ulId}\" style=\"display: none;\"" : '';
	$d = "<ul{$id}>";
	foreach($value as $k=>$v)
	{
		if(is_object($v)||is_array($v))
		{
			$newUlId = md5((time().rand(1000, 99999)));
			$k = (is_array($v) ? 'Array' : (is_object($v) ? get_class($v) : '')) . ': ' . $k . ( (count($v)>1) ? '(' . count($v) . ')' : '');
			$d .= "<li><a href=\"javascript:debugResultToggle('{$newUlId}');\">" . $k . '</a>' . showInner($v, $newUlId) . '</li>';
		}
		else
		{
			$d .= '<li><b>' . $k . '</b>: ' . $v . '</li>';
		}
	}
	$d .= '</ul>';
	return $d;
}

?>